/**************************************************************************
	Replication do-file: "Corruption in Customs"
	Cyril Chalendard, Ana Fernandes, Gael Raballand and Bob Rijkers
	
	Created on: 01/07/2022
**************************************************************************/

clear all
set more off, perm
cap log close
cls

* ----------------------- DIRECTORIES AND FOLDERS ----------------------- *

* Directories in which data are stored
global main "PUT YOUR DIRECTORY PATH HERE"
cd "$folder"

* Folders
global outputdata = "$main\Output Data"
cap mkdir "$main\Tables"
global tables = "$main\Tables"

* ----------------------------- BEGINS HERE ----------------------------- *

* -------
* Dataset
* -------
use "$outputdata\CFRR.dta", clear // open dataset


* -------
* Table 4
* -------

* Globals
global dep "ln_time fraud c1_dlog_val c1_dlog_tax htrl_f_int"
global controls "tax_rate risk red_i mixed_dec differentiated valitrade_advice"

* Matrix
foreach x in 1 2 5 {
	mat table4_`x' = J(6,5,.)
	mat table4_`x'T = J(1,5,.)
}
foreach x in 3 4 6 {
	mat table4_`x' = J(4,5,.)
	mat table4_`x'T = J(1,5,.)
}

* Loop for estimations
** Panel A
local i = 1
local j = 1
foreach x in $dep {

	qui: reghdfe `x' eis_f ln_familiarity $controls countryorig* max_twodigit* if sample_reg==1 , absorb(insp_f key_dec ym_*) vce(cluster insp_f key_dec)

	mat table4_1[`i',`j'] = _b[eis_f]
	local i = `i' + 1
	mat table4_1[`i',`j'] = _se[eis_f]
	local i = `i' + 1
	mat table4_1[`i',`j'] = _b[ln_familiarity]
	local i = `i' + 1
	mat table4_1[`i',`j'] = _se[ln_familiarity]
	mat table4_1T[1,`j'] = e(N)
	local i = `i' + 2
	mat table4_1[`i',`j'] = e(r2)
	local i = `i' -5
	local j = `j' +1
}
** Panel B
local i = 1
local j = 1
foreach x in $dep {

	qui: reghdfe `x' eis_f ln_congestion $controls countryorig* max_twodigit* if sample_reg==1 , absorb(insp_f key_dec ym_*) vce(cluster insp_f key_dec)

	mat table4_2[`i',`j'] = _b[eis_f]
	local i = `i' + 1
	mat table4_2[`i',`j'] = _se[eis_f]
	local i = `i' + 1
	mat table4_2[`i',`j'] = _b[ln_congestion]
	local i = `i' + 1
	mat table4_2[`i',`j'] = _se[ln_congestion]
	mat table4_2T[1,`j'] = e(N)
	local i = `i' + 2
	mat table4_2[`i',`j'] = e(r2)
	local i = `i' -5
	local j = `j' +1
}
** Panel C
local i = 1
local j = 1
foreach x in $dep {

	qui: reghdfe `x' eis_f $controls countryorig* max_twodigit* if sample_reg==1 & outside_bh!=1 , absorb(insp_f key_dec ym_*) vce(cluster insp_f key_dec)

	mat table4_3[`i',`j'] = _b[eis_f]
	local i = `i' + 1
	mat table4_3[`i',`j'] = _se[eis_f]
	mat table4_3T[1,`j'] = e(N)
	local i = `i' + 2
	mat table4_3[`i',`j'] = e(r2)
	local i = `i' -3
	local j = `j' +1
}
** Panel D
local i = 1
local j = 1
foreach x in $dep {

	qui: reghdfe `x' eis_f $controls countryorig* max_twodigit* if sample_reg==1 , absorb(insp_f key_dec ym_* key_importer) vce(cluster insp_f key_dec)

	mat table4_4[`i',`j'] = _b[eis_f]
	local i = `i' + 1
	mat table4_4[`i',`j'] = _se[eis_f]
	mat table4_4T[1,`j'] = e(N)
	local i = `i' + 2
	mat table4_4[`i',`j'] = e(r2)
	local i = `i' -3
	local j = `j' +1
}
** Panel E
local i = 1
local j = 1
foreach x in $dep {

	qui: reghdfe `x' eis_f eis_m_f $controls countryorig* max_twodigit* if sample_reg==1 & nbdec_m>=50 , absorb(insp_f key_dec ym_* key_importer) vce(cluster insp_f key_dec)

	mat table4_5[`i',`j'] = _b[eis_f]
	local i = `i' + 1
	mat table4_5[`i',`j'] = _se[eis_f]
	local i = `i' + 1
	mat table4_5[`i',`j'] = _b[eis_m_f]
	local i = `i' + 1
	mat table4_5[`i',`j'] = _se[eis_m_f]
	mat table4_5T[1,`j'] = e(N)
	local i = `i' + 2
	mat table4_5[`i',`j'] = e(r2)
	local i = `i' -5
	local j = `j' +1
}
** Panel F
local i = 1
local j = 1
foreach x in $dep {
	
	qui: reghdfe `x' eis_f $controls countryorig* max_twodigit* if sample_reg==1 & insprank>3 , absorb(insp_f key_dec ym_*) vce(cluster insp_f key_dec)

	mat table4_6[`i',`j'] = _b[eis_f]
	local i = `i' + 1
	mat table4_6[`i',`j'] = _se[eis_f]
	mat table4_6T[1,`j'] = e(N)
	local i = `i' + 2
	mat table4_6[`i',`j'] = e(r2)
	local i = `i' -3
	local j = `j' +1
}


* Export
* ------
putexcel set "$tables\Table 4.xlsx", replace sheet(Table4) // create a new excel spreadsheet

* Title
putexcel (A1:F1), merge hcenter vcenter
putexcel A1 = "Before delegated randomization of inspector assignment"

* Dependent variables
putexcel A2 = "Dependent variable", left vcenter
local cells `" "B" "C" "D" "E" "F" "'
local labels `" "Time" "Fraud" "\Delta log value" "\Delta log tax" "Hyp. tax revenue losses" "'
forvalues i = 1(1)5 {

	local x: word `i' of `labels'
	local y: word `i' of `cells'

	putexcel `y'2 = "`x'", hcenter vcenter bold	
}

* Panels
** Name
local cells `" "4" "13" "22" "29" "36" "45" "'
local labels `" "A. Controlling for familiarity" "B. Controlling for congestion" "C. Excluding declarations registered outside regular business hours" "D. Adding importer fixed effects" "E. Adding importer fixed effects and excess interaction share with importers" "F. Dropping top 3 inspectors with the largest share of declarations with excess interaction each semester" "'
forvalues i = 1(1)6 {

	local x: word `i' of `labels'
	local y: word `i' of `cells'

	putexcel (A`y':F`y'), merge hcenter vcenter bold
	putexcel A`y' = "`x'"
}
** Main independent variable
local i = 1
foreach x in 6 15 24 31 38 47 {
	putexcel A`x' = "Excess interaction share", left vcenter
	putexcel B`x' = matrix(table4_`i'), nformat(0.000) hcenter vcenter
	local i = `i' +1
}
** Observations
local i = 1
foreach x in 10 19 26 33 42 49 {
	putexcel A`x' = "Observations", left vcenter
	putexcel B`x' = matrix(table4_`i'T), nformat(number_sep) hcenter vcenter
	local i = `i' +1
}
** R2
foreach x in 11 20 27 34 43 50 {
	putexcel A`x' = "R-squared", left vcenter
}
** Column numbers
global cells "B C D E F B C D E F B C D E F B C D E F B C D E F B C D E F"
forvalues i = 1(1)30 {
	
	local y: word `i' of $cells

	if `i' < 6 {
		putexcel `y'5 = "(`i')", hcenter vcenter
	}
	
	if `i' > 5 & `i' < 11 {
		putexcel `y'14 = "(`i')", hcenter vcenter
	}
	
	if `i' > 10 & `i' < 16 {
		putexcel `y'23 = "(`i')", hcenter vcenter
	}
	
	if `i' > 15 & `i' < 21 {
		putexcel `y'30 = "(`i')", hcenter vcenter
	}
	
	if `i' > 20 & `i' < 26 {
		putexcel `y'37 = "(`i')", hcenter vcenter
	}
	
	else {
		putexcel `y'46 = "(`i')", hcenter vcenter
	}
}
macro drop cells
** Additional independent variable
local cells `" "8" "17" "40" "'
local labels `" "Familiarity" "Congestion" "Excess interaction share with importer" "'
forvalues i = 1(1)3 {

	local x: word `i' of `labels'
	local y: word `i' of `cells'

	putexcel A`y' = "`x'", left vcenter
}


* -------------------------------- ENDS HERE -------------------------------- *